﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;

namespace Savage.DotNetLibrary.WebLibrary
{
    public class MachineKeyGenerator
    {
        public MachineKeyGenerator()
        {
            _validationKey = CreateKey(64);
            _decryptionKey = CreateKey(24);
        }
        
        private string _validationKey;
        public string ValidationKey
        {
            get { return _validationKey; }
        }

        private string _decryptionKey;
        public string DecryptionKey
        {
            get { return _decryptionKey; }
        }

        private static String CreateKey(int numBytes)
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[numBytes];

            rng.GetBytes(buff);
            return BytesToHexString(buff);
        }

        private static String BytesToHexString(byte[] bytes)
        {
            StringBuilder hexString = new StringBuilder(64);

            for (int counter = 0; counter < bytes.Length; counter++)
            {
                hexString.Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", bytes[counter]));
            }
            return hexString.ToString();
        }
    }
}
